/*
 * Copyright (c) ycq_work@163.com
 */

package com.mxg.blog.common;

import com.mxg.blog.entity.Article;
import com.mxg.blog.service.ArticleService;

import java.util.List;

/**
 * @Author: ycq_work@163.com
 * @DATE: 2023/6/25 13:58
 * 文章管理的一些公用处理
 */
public class ArticleOperate {
    CqLogger cqLogger = CqLogger.getInstance();

    private String requestMapUserAccount;

    // 设置文章id按文章id查找文章
    private Integer articleId;

    // 设置文章articleService
    private ArticleService articleService;

    //
    private static volatile ArticleOperate instance;

    ArticleOperate(){

    }

    ArticleOperate(Integer articleId) {
        this.articleId = articleId;
    }


    // 设置文章id
    public void setArticleId(Integer articleId) {
        this.articleId = articleId;
    }

    private void setArticleService(ArticleService articleService) {
        this.articleService = articleService;
    }

    // 携带articleId创建类
    public static ArticleOperate getInstance(Integer articleId, ArticleService articleService) {
        if (instance == null) {
            synchronized (ArticleOperate.class) {
                if (instance == null) {
                    instance = new ArticleOperate(articleId);
                }
            }
        }
        instance.setArticleId(articleId);
        instance.setArticleService(articleService);
        return instance;
    }

    // 重载 没有articleId创建类
    public static ArticleOperate getInstance(ArticleService articleService){
        if(instance == null){
            synchronized (ArticleOperate.class){
                if(instance == null){
                    instance = new ArticleOperate();
                }
            }
        }
        instance.setArticleService(articleService);
        return instance;
    }

    // 获取文章, 根据文章Id
    public Article getArticle(){
        try{
            cqLogger.print("查找完成");
            return articleService.getArticleByArticleId(articleId);
        }catch (Exception e){
            cqLogger.print("查找错误" + e);
        }
        return null;
    }

    // 获取所有已发布文章
    public List<Article> getArticles(){
        try{
            return articleService.getArticleByUserIdAndStatus1(1);
        }catch (Exception e){
            cqLogger.print("查找失败" + e);
        }
        return null;
    }
}
